Path: blob/master/src/smc_pyutil/smc_pyutil/new_file.py
Views: 285
#!/usr/bin/python1# -*- coding: utf-8 -*-23# This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.4# License: AGPLv3 s.t. "Commons Clause" – read LICENSE.md for details56from __future__ import absolute_import7from __future__ import print_function8import os, platform, shutil, sys910PLATFORM = platform.system().lower()111213def new_file(path):14if os.path.exists(path):15# nothing to do.16return1718base, filename = os.path.split(path)1920if base and not os.path.exists(base):21os.makedirs(base)2223ext = os.path.splitext(path)[1].lower()24for places in [25os.environ['HOME'],26os.path.dirname(os.path.realpath(__file__))27]:28template = os.path.join(places, 'templates', PLATFORM, 'default' + ext)29if os.path.exists(template):30shutil.copyfile(template, path)31return3233# No template found34open(path, 'w').close()353637def main():38if len(sys.argv) == 1:39print("""40This script is called like so:4142%s path/to/file.tex another/path/to/a/file.tex ....4344If path/to/file.tex already exists, nothing happens.45If path/to/file.tex does not exist, it is created (including the directory that contains it),46and if there is a file $HOME/templates/default.tex or /projects/templates/[platform]/default.tex (for tex extension),47then that template file is set to the initial contents. """ % sys.argv[0])48sys.exit(1)4950for x in sys.argv[1:]:51new_file(x)525354if __name__ == "__main__":55main()565758