gns-deb-diff

automate the documentation of the differences between gNewSense and Debian.
git clone git://git.ricketyspace.net/gns-deb-diff.git
Log | Files | Refs

commit 981267a35e44fea8804ccf249ff89d7850f87703
parent f4e2735f87691f8b5e748ea4de33954c1665992c
Author: rsiddharth <s@ricketyspace.net>
Date:   Wed,  9 Nov 2016 04:19:25 +0000

Add `write_file` function.

* gd_diff.py (write_file): New function.
(save_gns_readme, slurp_gns_readme): Update functions.
* tests/test_gd_diff.py (test_write_file): New method.
(setup, test_save_gns_readme, test_save_gns_readme_double)
(teardown): Update methods

Diffstat:
gd_diff.py | 23+++++++++++++++++------
tests/test_gd_diff.py | 22++++++++++++++++++++--
2 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/gd_diff.py b/gd_diff.py @@ -43,6 +43,20 @@ def read_file(fpath): return f.read() +def write_file(fpath, content): + """Write `content` to file `fpath`. + + """ + try: + f = open(fpath, 'w') + f.write(content) + f.close() + except IOError as e: + print('Error creating and writing content to {}\n {}'.format( + fpath, e)) + exit(1) + + def execute(cmd, out=None, err=None): """Run `cmd`. Returns an instance of `subprocess.CompletedProcess` @@ -119,11 +133,8 @@ def save_gns_readme(content, release, pkg, local_dir): sys.exit(1) gns_readme = path.join(gns_readme_dir, 'README.gNewSense') - - with open(gns_readme, 'wb') as f: - f.write(content) - f.flush() - print('Saved {}'.format(gns_readme)) + write_file(gns_readme, content) + print('Saved {}'.format(gns_readme)) def slurp_gns_readme(release, pkg, local_dir): @@ -136,7 +147,7 @@ def slurp_gns_readme(release, pkg, local_dir): cp = execute(cmd, out=PIPE, err=PIPE) if(cp.returncode == 0): - save_gns_readme(cp.stdout, release, pkg, local_dir) + save_gns_readme(cp.stdout.decode(), release, pkg, local_dir) return True else: print("README.gNewSense not found for package {}".format(pkg), diff --git a/tests/test_gd_diff.py b/tests/test_gd_diff.py @@ -28,20 +28,35 @@ class TestGdDiff(object): self.small_pkgs_file = 'tests/files/small-parkes-pkgs.list' self.pkgs_file_ne = 'tests/nonexistent-file.list' self.gns_pkgs_dir = 'tests/gns-pkgs' + self.test_home = 'tests/HOME' + self.test_w_file = os.path.join(self.test_home, 'w_file') + self.stderr_orig = sys.stderr + # make test home + os.mkdir(self.test_home, mode=0o700) + + def test_read_file_success(self): f_content = read_file(self.pkgs_file) assert isinstance(f_content, str) assert_equal(len(f_content.split('\n')), 82) + @raises(SystemExit) def test_read_file_error(self): with open(os.devnull, 'w') as sys.stderr: f_content = read_file(self.pkgs_file_ne) + def test_write_file(self): + content = 'One Goodbye\n Stealing Romance' + write_file(self.test_w_file, content) + + assert read_file(self.test_w_file) == content + + def test_execute_success(self): cmd = 'python --version' cp = execute(cmd, out=subprocess.PIPE) @@ -92,7 +107,7 @@ class TestGdDiff(object): def test_save_gns_readme(self): cmd = 'bzr cat bzr://bzr.sv.gnu.org/gnewsense/packages-parkes/antlr/debian/README.gNewSense' cp = execute(cmd, out=subprocess.PIPE) - readme_content = cp.stdout + readme_content = cp.stdout.decode() # convert to str # save it save_gns_readme(readme_content, 'parkes', 'antlr', self.gns_pkgs_dir) @@ -105,7 +120,7 @@ class TestGdDiff(object): def test_save_gns_readme_double(self): cmd = 'bzr cat bzr://bzr.sv.gnu.org/gnewsense/packages-parkes/antlr/debian/README.gNewSense' cp = execute(cmd, out=subprocess.PIPE) - readme_content = cp.stdout + readme_content = cp.stdout.decode() # convert to str # save it twice save_gns_readme(readme_content, 'parkes', 'antlr', self.gns_pkgs_dir) @@ -220,5 +235,8 @@ class TestGdDiff(object): os.chmod(self.gns_pkgs_dir, mode=0o700) rmtree(self.gns_pkgs_dir) + if(path.exists(self.test_home)): + rmtree(self.test_home) + # restore sys.stderr sys.stderr = self.stderr_orig