From 5d10ec501116b99a77c8a48856f5fe224ace8f74 Mon Sep 17 00:00:00 2001 From: Suyono Date: Thu, 24 Nov 2022 09:05:08 +1100 Subject: [PATCH] WIP: initial commit --- .gitignore | 1 + .idea/.gitignore | 8 +++++ .idea/misc.xml | 4 +++ .idea/modules.xml | 8 +++++ .idea/tarroot.iml | 10 +++++++ .idea/vcs.xml | 6 ++++ tarroot.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 111 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/tarroot.iml create mode 100644 .idea/vcs.xml create mode 100644 tarroot.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d488f5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/venv/* diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4cef9e2 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ba58d14 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/tarroot.iml b/.idea/tarroot.iml new file mode 100644 index 0000000..285104c --- /dev/null +++ b/.idea/tarroot.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/tarroot.py b/tarroot.py new file mode 100644 index 0000000..7304bb0 --- /dev/null +++ b/tarroot.py @@ -0,0 +1,74 @@ +# This is a sample Python script. + +# Press ⌃R to execute it or replace it with your code. +# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings. + +import argparse +import os +import tarfile + + +class TarRoot: + def __init__(self, arguments): + self.debug = arguments.debug + self.target_dir = arguments.target_dir + self.tar = tarfile.open(arguments.target_tar, "w:gz") + + def debug_print(self, *arguments): + if self.debug: + output = "" + for arg in arguments: + output += repr(arg) + print("DEBUG: " + output) + + def run(self): + start_dir = os.getcwd() + self.debug_print("entering target dir: ", self.target_dir) + os.chdir(self.target_dir) + self._run('.') + self.debug_print("returning to original dir: ", start_dir) + os.chdir(start_dir) + + def _append_dir(self, entry, *parent): + path = entry.name + if len(parent) > 0: + path = parent[0] + os.sep + path + ti = tarfile.TarInfo(path) + ti.type = tarfile.DIRTYPE + stat_result = entry.stat(follow_symlinks=False) + ti.mode = stat_result.st_mode + ti.uid = stat_result.st_uid + ti.gid = stat_result.st_gid + ti.mtime = stat_result.st_mtime + self.debug_print("writing tar info ", ti) + self.tar.addfile(ti) + + def _append_file(self, entry): + pass + + def _run(self, running_dir): + symlinks = [] + with os.scandir(running_dir) as it: + for entry in it: + if entry.is_symlink(): + self.debug_print("appending symlink ", entry) + symlinks.append(entry) + elif entry.is_dir(): + self.debug_print("appending directory ", entry) + self._append_dir(entry, running_dir) if running_dir != "." else self._append_dir(entry) + self.debug_print("going recursive") + self._run(running_dir + os.sep + entry.name) if running_dir != "." else self._run(entry.name) + else: + self.debug_print("appending file ", entry) + self._append_file(entry) + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument("--debug", help="debug flag", action="store_true") + parser.add_argument("target_dir") + parser.add_argument("target_tar") + args = parser.parse_args() + tr = TarRoot(args) + tr.run()