11import os
2+ import smtplib
3+ import stat
24import sys
35import zipfile
6+ from email .message import EmailMessage
7+ from multiprocessing import Process
48from pathlib import Path
5- from subprocess import call
9+ from subprocess import call , check_output
610from typing import *
711
812import evalfen
9- from getFile import downloadName , getAvailableNames
13+ from getFile import createDir , downloadName , getAvailableNames
1014from send import sendFile , sendNotification
1115
1216STOCKFISH_DOWNLOAD = {
1620 "darwin" : "https://stockfishchess.org/files/stockfish-11-mac.zip"
1721}
1822
23+ STOCKFISH_LOCATION = {
24+ "win32" : r"stockfish\stockfish-11-win\Windows\stockfish_20011801_x64_bmi2.exe" ,
25+ "linux" : "stockfish/stockfish-11-linux/Linux/stockfish_20011801_x64_bmi2" ,
26+ "linux32" : "stockfish/stockfish-11-linux/Linux/stockfish_20011801_x64_bmi2" ,
27+ "darwin" : "stockfish/stockfish-11-mac/Mac/stockfish-11-"
28+ }
29+
30+ def promptName () -> str :
31+ return input ("What is your name or github username? " )
1932
20- def unzip (filepath , resultpath ):
33+
34+ def unzip (filepath : str , resultpath : str ) -> None :
2135 with zipfile .ZipFile (filepath , 'r' ) as zip_ref :
2236 zip_ref .extractall (resultpath )
2337
@@ -38,25 +52,89 @@ def downloadStockfish() -> None:
3852 link = STOCKFISH_DOWNLOAD [sys .platform ]
3953 call (["curl" , "-o" , "stockfish.zip" , link ])
4054 unzip ("stockfish.zip" , "stockfish/" )
41-
55+ stockfishexecutable = str (findStockfish ())
56+ if sys .platform != "win32" :
57+ os .chmod (stockfishexecutable , stat .S_IEXEC )
58+ os .remove ("stockfish.zip" )
59+
60+ def promptNameChoice () -> Tuple [str ]:
61+ available = os .listdir ("data" )
62+ for i , name in enumerate (available ):
63+ print (i , name , sep = '\t ' )
64+ uin = input ("Which would you like to start on? " )
65+ try :
66+ uin = available [int (uin )]
67+ except ValueError :
68+ pass
69+ return str (Path (os .getcwd ()) / "data" / uin ), uin
4270
4371def findStockfish () -> Path :
44- pass
72+ toadd = "bmi2"
73+ if sys .platform == "darwin" and \
74+ '-3' in check_output (["/usr/sbin/sysctl" , "-n" , "machdep.cpu.brand_string" ]).decode ():
75+ toadd = "modern"
76+ return Path (os .getcwd ()) / (STOCKFISH_LOCATION [sys .platform ] + toadd )
4577
4678
4779def promptStockfish () -> Path :
48- uin = input ("Do you have stockfish in your current directory?(y)es/(n)o " )
49- needsfish = 'y' in uin
80+ needsfish = "stockfish" not in os .listdir ()
5081 if needsfish :
82+ print ("Downloading stockfish" )
5183 downloadStockfish ()
52-
84+ return findStockfish ()
85+
86+ def getNumberThreads () -> int :
87+ return os .cpu_count ()
88+
89+ def progressBar (percentage : float ) -> str :
90+ numpound = round (percentage * 10 )
91+ numdash = 10 - numpound
92+ return '[' + numpound * '#' + numdash * '-' + ']\t ' + "%.2f" % (percentage * 100 ) + "%"
93+
94+ def countOutput (count : int , length : int ) -> None :
95+ print (progressBar (count / length ), end = '\r ' , flush = True )
96+
97+ def promptThreads () -> int :
98+ numthreads = getNumberThreads ()
99+ userthreads = 0
100+ print (f"You have { numthreads } threads available." )
101+ while not 1 <= userthreads <= numthreads :
102+ try :
103+ userthreads = int (input ("How many threads would you like to use? " ))
104+ except ValueError :
105+ pass
106+ return userthreads
53107
54108def main () -> None :
109+ createDir ("dest" )
110+ createDir ("data" )
55111 if promptDownload ():
56112 names = list (getAvailableNames ())
57113 names .sort ()
58114 name = promptNames (names )
59115 downloadName (name )
116+ sendNotification (promptName (), name )
117+ pathToStockfish = str (promptStockfish ())
118+ threads = promptThreads ()
119+ source , name = promptNameChoice ()
120+ dest = str (Path (os .getcwd ()) / "dest" / name )
121+
122+ amountlines = evalfen .lineCount (dest )
123+ finallines = evalfen .lineCount (source )
124+ countOut = lambda c : countOutput (c , finallines )
125+
126+ evalargs = (source , dest , 22 , threads , amountlines , pathToStockfish , countOut )
127+ evaluate = Process (target = evalfen .main , args = evalargs )
128+ evaluate .start ()
129+
130+ print ("Control c to quit" )
131+ try :
132+ evaluate .join ()
133+ print (dest )
134+ sendFile (promptName (), dest )
135+ except KeyboardInterrupt :
136+ evaluate .terminate ()
137+
60138 print ("Done for now" )
61139
62140
0 commit comments