Skip to content

Commit 124e3d2

Browse files
committed
Merge branch 'contributingApp'
Finished the contributing app, need built versions
2 parents ed9b245 + f5d9135 commit 124e3d2

6 files changed

Lines changed: 135 additions & 37 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ stockfish/
66
stockfish.zip
77
__MAC*
88
*cpython*
9+
data/
10+
dest/

contribute/auto.py

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import os
2+
import smtplib
3+
import stat
24
import sys
35
import zipfile
6+
from email.message import EmailMessage
7+
from multiprocessing import Process
48
from pathlib import Path
5-
from subprocess import call
9+
from subprocess import call, check_output
610
from typing import *
711

812
import evalfen
9-
from getFile import downloadName, getAvailableNames
13+
from getFile import createDir, downloadName, getAvailableNames
1014
from send import sendFile, sendNotification
1115

1216
STOCKFISH_DOWNLOAD = {
@@ -16,8 +20,18 @@
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

4371
def 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

4779
def 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

54108
def 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

contribute/evalfen.py

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import sys
33
from threading import Thread
4+
import subprocess
45

56
import chess
67
import chess.engine
@@ -35,7 +36,7 @@ def evalFENThread(output, i, fen, engine, d):
3536
info = engine.analyse(board, chess.engine.Limit(depth=d))
3637
output[i] = str(info["score"].white()) + '\n'
3738

38-
def main(filein, fileout, d, threads, linetostart, enginepath):
39+
def main(filein, fileout, d, threads, linetostart, enginepath, counterOutput = lambda x: print(x, end='\r', flush=True)):
3940
engines = [chess.engine.SimpleEngine.popen_uci(enginepath) for i in range(threads)]
4041

4142
with open(filein, 'r') as fin:
@@ -46,35 +47,41 @@ def main(filein, fileout, d, threads, linetostart, enginepath):
4647

4748
counter = linetostart
4849
with open(fileout, 'a+') as fout:
49-
while counter < l:
50-
ts = []
51-
threadcontents = [0 for i in range(threads)]
52-
for i in range(threads):
53-
try:
54-
f = contents.pop(0)
55-
fen = f[:-1]
56-
t = Thread(
57-
target = evalFENThread,
58-
args = (threadcontents, i, fen, engines[i], d),
59-
daemon = True
60-
)
61-
ts.append(t)
62-
except IndexError:
63-
print("Almost done")
64-
for t in ts:
65-
t.start()
50+
try:
51+
while counter < l:
52+
ts = []
53+
threadcontents = [0 for i in range(threads)]
54+
for i in range(threads):
55+
try:
56+
f = contents.pop(0)
57+
fen = f[:-1]
58+
t = Thread(
59+
target = evalFENThread,
60+
args = (threadcontents, i, fen, engines[i], d),
61+
daemon = True
62+
)
63+
ts.append(t)
64+
except IndexError:
65+
print("Almost done")
66+
for t in ts:
67+
t.start()
68+
for t in ts:
69+
t.join()
70+
for c in threadcontents:
71+
try:
72+
fout.write(c)
73+
fout.flush()
74+
except:
75+
if c != 0:
76+
print("Add this:", c)
77+
counter += threads
78+
counterOutput(counter)
79+
del ts, threadcontents
80+
except KeyboardInterrupt:
81+
print()
82+
print("Caught Signal, beginning quit process")
6683
for t in ts:
6784
t.join()
68-
for c in threadcontents:
69-
try:
70-
fout.write(c)
71-
fout.flush()
72-
except:
73-
if c != 0:
74-
print("Add this:", c)
75-
counter += threads
76-
print(counter)
77-
del ts, threadcontents
7885

7986
print("Done")
8087
for engine in engines: engine.quit()

contribute/getFile.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from typing import *
2+
from pathlib import Path
3+
import os
24

35
import requests
46

@@ -27,10 +29,17 @@ def getAvailableNames() -> Set[str]:
2729
taken = getTakenNames()
2830
return {name for name in allnames if name not in taken}
2931

32+
def createDir(name: str) -> None:
33+
try:
34+
os.mkdir(name)
35+
except FileExistsError:
36+
pass
37+
3038
def downloadName(name: str) -> None:
39+
createDir("data")
3140
fenurl = "https://raw.githubusercontent.com/r2dev2bb8/ChessData/master/fens/"
3241
r = requests.get(fenurl + name, HEADERS)
33-
with open("../" + name, 'w+') as fout:
42+
with open(str(Path(os.getcwd()) / "data" / name), 'w+') as fout:
3443
fout.write(r.content.decode())
3544

3645
def main():

contribute/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
dill==0.3.1.1
2+
requests==2.23.0

contribute/send.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def sendFile(username: str, filename: str) -> None:
1414

1515
def main():
1616
send("Test mail")
17+
# sendFile("r2dev2", "dest/Berliner.txt")
1718

1819
if __name__ == "__main__":
1920
main()

0 commit comments

Comments
 (0)