Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cba00f

Browse files
committed
linux working
1 parent 80a5d2b commit 2cba00f

File tree

5 files changed

+89
-82
lines changed

5 files changed

+89
-82
lines changed

src/codebots/bots/drivebot.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import authentication
2-
from googleapiclient.discovery import build
3-
from googleapiclient.http import MediaFileUpload
41
import os
52
from pydrive.drive import GoogleDrive
63
from pydrive.auth import GoogleAuth
74
from codebots import SECRETS
85

96

10-
from ._bot import BaseBot
11-
127
__all__ = [
138
'DriveBot'
149
]

src/codebots/bots/latexbot.py

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import os
2-
import importlib
31
import subprocess
2+
from subprocess import Popen, PIPE
43
import tempfile
54
from pathlib import Path
65
from sys import platform
7-
from threading import local
8-
6+
from getpass import getpass
97

108
from ._bot import BaseBot
119
from ..utilities.web_tools import download_file
@@ -21,7 +19,7 @@ class LatexBot(BaseBot):
2119
"""LatexBot.
2220
"""
2321

24-
def __init__(self, config_file=None, sender=None) -> None:
22+
def __init__(self) -> None:
2523
self.__name__ = "latexbot"
2624

2725
def install_dependencies(self, git, pandoc, miktex):
@@ -32,7 +30,13 @@ def install_dependencies(self, git, pandoc, miktex):
3230
3331
Parameters
3432
----------
35-
None
33+
git : bool
34+
if True install git
35+
pandoc : bool
36+
if True install pandoc
37+
miktex : bool
38+
if True install miktex
39+
3640
3741
Kwargs
3842
------
@@ -52,7 +56,16 @@ def install_dependencies(self, git, pandoc, miktex):
5256
try:
5357

5458
if platform == "linux" or platform == "linux2": # linux
55-
raise NotImplementedError("your os is currently not supported")
59+
if git:
60+
print("git is pre-installed in linux")
61+
if miktex:
62+
print("NotImplemented")
63+
settings = {
64+
'pandoc':
65+
{'url': 'https://github.com/jgm/pandoc/releases/download/2.15/pandoc-2.15-1-amd64.deb',
66+
'function': 'install_deb',
67+
'file_name': 'pandoc-2.15-1-amd64.deb'},
68+
}
5669
elif platform == "darwin": # OS X
5770
raise NotImplementedError("your os is currently not supported")
5871
elif platform == "win32": # Windows
@@ -86,6 +99,12 @@ def install_dependencies(self, git, pandoc, miktex):
8699
temp_dir.cleanup()
87100
raise Exception("ERROR, something went wrong")
88101

102+
def add_overleaf_project(self, name, code):
103+
raise NotImplementedError()
104+
105+
def add_document(self, name, path):
106+
raise NotImplementedError()
107+
89108
def _clone_overleaf_temp(self, document_code):
90109
"""Temporarily clone the overleaf project on the local machine.
91110
@@ -140,14 +159,29 @@ def convert_tex_to_docx(self, input_path, output_path=None, open_docx=False):
140159
open_docx = True
141160
else:
142161
output = Path().joinpath(output_path, str(file.name).split('.tex')[0]+'.docx')
143-
out = subprocess.run(['pandoc', '-o', output, '-t', 'docx', file])
144-
print(f"docx saved in {output}")
162+
if platform == "linux" or platform == "linux2":
163+
password = getpass("Please enter your password: ")
164+
# sudo requires the flag '-S' in order to take input from stdin
165+
proc = Popen(f"sudo -S pandoc -o {output} -t docx {file}".split(), stdin=PIPE, stdout=PIPE, stderr=PIPE)
166+
# Popen only accepts byte-arrays so you must encode the string
167+
stdout, stderr = proc.communicate(password.encode())
168+
print(stdout.decode())
169+
print(stderr.decode())
170+
# out = subprocess.run(['sudo', 'pandoc', '-o', output, '-t', 'docx', file])
171+
else:
172+
out = subprocess.run(['pandoc', '-o', output, '-t', 'docx', file])
173+
if out.returncode == 0:
174+
print(f"docx saved in {output}")
175+
else:
176+
raise Exception("could not save docx")
145177
output_paths.append(output)
146-
# print("The exit code was: %d" % out.returncode)
147178
if open_docx:
148179
print("opening file... don't forget to save it (save as...)!")
149-
p = subprocess.Popen(output, shell=True)
150-
p.wait()
180+
if platform == "linux" or platform == "linux2": # linux
181+
print("Not yet supported!") # p = subprocess.run(["libreoffice", output])
182+
else:
183+
p = subprocess.Popen(output, shell=True)
184+
p.wait()
151185
return output_paths
152186

153187
def convert_overleaf_to_docx(self, document_code, output_path=None, open_docx=True, upload=False):
@@ -166,6 +200,14 @@ def convert_overleaf_to_docx(self, document_code, output_path=None, open_docx=Tr
166200
open_docx : bool, optional
167201
open the docx file after creation, by default True. Use this to
168202
inspect the conversion and save the file in a different location.
203+
upload : bool, optional
204+
uploads the document to your google drive, by default False.
205+
206+
Notes
207+
-----
208+
In order for the upload to succeed, you must configure the `drivebot`
209+
first by running `drivebot configure -type=web -save=True` in the command
210+
line.
169211
"""
170212
temp_dir = self._clone_overleaf_temp(document_code)
171213
try:

src/codebots/cli/cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,17 @@ def authenticate(type, save):
433433
click.echo('done!')
434434

435435

436+
@drivebot.command()
437+
@click.argument('path_to_file')
438+
@click.argument('name')
439+
def upload_local_file(path_to_file, name):
440+
"""Upload a local file to your Google Drive.\n
441+
"""
442+
bot = DriveBot('web')
443+
bot.upload_local_file(path_to_file, name)
444+
click.echo('done!')
445+
446+
436447
# -------------------------------- DEBUG ----------------------------------#
437448
if __name__ == "__main__":
438449
sys.exit(main())

src/codebots/utilities/google.py

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/codebots/utilities/os_tools.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import subprocess
2+
import os
23

34

45
### ------------------------ CROSS PLATFORM ---------------------------------###
@@ -50,14 +51,29 @@ def install_exe(file_path):
5051
print("installation complete")
5152
return out
5253

54+
### ------------------------------ LINUX ------------------------------------###
5355

56+
57+
def install_deb(file_path):
58+
out = subprocess.run(["sudo", "dpkg", "-i", file_path])
59+
if out.returncode == 0:
60+
print("installation complete")
61+
62+
63+
def register_GPG_key(keyserver="hkp://keyserver.ubuntu.com:80", recv_key="D6BC243565B2087BC3F897C9277A7293F59E4889"):
64+
out = subprocess.run(["sudo", "apt-key", "adv", "--keyserver", keyserver, "--recv-keys", recv_key])
65+
return out
66+
67+
68+
def install_miktex_ubuntu(url="http://miktex.org/download/ubuntu bionic universe"):
69+
os.system(f"echo deb {url} | sudo tee /etc/apt/sources.list.d/miktex.list")
70+
os.system("sudo apt-get update")
71+
os.system("sudo apt-get install miktex")
72+
73+
# out=subprocess.run(["sudo", "aptpkey", "adv", "--keyserver", keyserver, "--recv-keys", recv_key])
74+
# if out.returncode == 0:
75+
# print("installation complete")
5476
### ------------------------------ DEBUG ------------------------------------###
5577
if __name__ == "__main__":
56-
import os
57-
# print(is_tool("latexmk"))
58-
# install_exe('c:/temp/miktex.exe')
59-
p = subprocess.Popen('C:/temp/main.docx', shell=True)
60-
p.wait()
61-
# os.startfile('C:/temp/main.docx')
62-
63-
print('ciao')
78+
register_GPG_key()
79+
install_miktex_ubuntu()

0 commit comments

Comments
 (0)