Compare commits

...

3 Commits

Author SHA1 Message Date
Darksider3 1cbfe098a2 Backup.py: On our road to PEP8-conformity, rename and docstring!
snake_case-convention even boils down to the modules/files being named
after it.
Also, i added some simple header docstring, describing the module(albeit
short, it's currently it...)
4 years ago
Darksider3 67639ab7f7 Backup.py: Rename to conform to snake_case convention
Which will cover my ass down the road getting this code being told great
by PEP standards
4 years ago
Darksider3 9937381f18 Create /app/user/.ssh before trying to write to it! 4 years ago

@ -1,44 +0,0 @@
FROM python:3-slim
MAINTAINER n1trux
RUN apt-get update &&\
apt-get -y upgrade &&\
DEBIAN_FRONTEND=noninteractive apt-get -y install \
nano rsync openssh-server acl
# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# create user for applications
RUN useradd -Md /app/user/ -s /app/user/userapplication.py tilde
# make tilde's password empty
RUN passwd -d tilde
RUN usermod -U tilde
# add admin user
RUN useradd -Md /app/admin -s /app/admin/administrate.py admin
# privilege separation directory
RUN mkdir -p /var/run/sshd
# expose SSH port
EXPOSE 22
ENV TILDE_CONF="/app/data/applicationsconfig.ini"
# private/{scripts, administrate.py}, public/{scripts, userapplications.py}, config/userapplicatonsconfig.ini
#configs, logs, db
COPY config/applicationsconfig.ini /app/data/applicationsconfig.ini
#SSH config into /etc :)
COPY config/etc /etc
RUN touch /app/data/applications.sqlite
RUN touch /app/data/applications.log
# Doesnt work, @TODO why
#RUN setfacl -R -m u:tilde:rwx /app/data/
RUN chown -R tilde /app/data
# admin scripts
COPY private/ /app/admin/
# user accessible scripts
# Make TILDE_ENV
COPY public/ /app/user/
RUN mkdir /app/user/.ssh
CMD ["sh", "-c", " echo TILDE_CONF=$TILDE_CONF > /app/user/.ssh/environment && exec /usr/sbin/sshd -D"]

@ -1,4 +1,6 @@
#!/usr/bin/env python3
""" This module is thought to be the main point to export and import users.
It's actually not really a module but a script ought to be run from the command line"""
import configparser
import csv
@ -12,7 +14,7 @@ class Backup:
"""Backups a Tilde database to an CSV file
:Example:
>>> from Backup import Backup
>>> from backup import Backup
>>> from ListUsers import ListUsers
>>> L = ListUsers.ListUsers("/path/to/sqlite").get_fetch()
>>> backup_db = Backup("stdout")
@ -37,12 +39,13 @@ class Backup:
:type dialect: str
"""
self.setFilename(output)
self.setQuoting(quoting)
self.setDialect(dialect)
self.setFieldnames(tuple(['id', 'username', 'email', 'name', 'pubkey', 'timestamp', 'status']))
self.set_filename(output)
self.set_quoting(quoting)
self.set_dialect(dialect)
self.set_field_names(tuple(['id', 'username', 'email', 'name',
'pubkey', 'timestamp', 'status']))
def setDialect(self, dialect: str) -> None:
def set_dialect(self, dialect: str) -> None:
""" Set dialect for Object
:param dialect: Dialect to set for Object
@ -53,7 +56,7 @@ class Backup:
self.dialect = dialect
def setQuoting(self, quoting: int) -> None:
def set_quoting(self, quoting: int) -> None:
""" Set quoting in the CSV(must be supported by the CSV Module!)
:param quoting: Quoting Integer given by csv.QUOTE_* constants
@ -64,7 +67,7 @@ class Backup:
self.quoting = quoting
def setFilename(self, filename: str) -> None:
def set_filename(self, filename: str) -> None:
""" Sets Filename to output to
:param filename: Filename to output to(set stdout for stdout)
@ -75,8 +78,8 @@ class Backup:
self.filename = filename
def setFieldnames(self, f_names: tuple) -> None:
""" Set fieldname to process
def set_field_names(self, f_names: tuple) -> None:
""" Set field name to process
:param f_names: Fieldnames-Tuple
:type f_names: tuple
@ -95,20 +98,22 @@ class Backup:
"""
returner = io.StringIO()
write_csv = csv.DictWriter(returner, fieldnames=self.field_names, quoting=self.quoting, dialect=self.dialect)
write_csv = csv.DictWriter(returner, fieldnames=self.field_names,
quoting=self.quoting, dialect=self.dialect)
write_csv.writeheader()
for row in fetched:
write_csv.writerow(dict(row))
# sqlite3.Row doesn't "easily" convert to a dict itself sadly, so just a quick help from us here
# it actually even delivers a list(sqlite3.Row) also, which doesnt make the life a whole lot easier
# sqlite3.Row doesn't "easily" convert to a dict itself sadly,
# so just a quick help from us here
# it actually even delivers a list(sqlite3.Row) also,
# which doesnt make the life a whole lot easier
if self.filename == "stdout":
print(returner.getvalue())
return True
else:
with open(self.filename, "w") as f:
print(returner.getvalue(), file=f)
return True
return True
if __name__ == "__main__":
@ -121,7 +126,7 @@ if __name__ == "__main__":
fetch = L.get_fetch()
if fetch:
B = Backup(args.file)
B.setFieldnames(fetch[0].keys()) # sqlite3.row delivers its keys for us! SO NICE!
B.set_field_names(fetch[0].keys()) # sqlite3.row delivers its keys for us! SO NICE!
B.backup_to_file(fetch)
else:
print("nothing to backup!")
Loading…
Cancel
Save