Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/smc_pyutil/smc_pyutil/new_file.py
Views: 285
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
# This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
5
# License: AGPLv3 s.t. "Commons Clause" – read LICENSE.md for details
6
7
from __future__ import absolute_import
8
from __future__ import print_function
9
import os, platform, shutil, sys
10
11
PLATFORM = platform.system().lower()
12
13
14
def new_file(path):
15
if os.path.exists(path):
16
# nothing to do.
17
return
18
19
base, filename = os.path.split(path)
20
21
if base and not os.path.exists(base):
22
os.makedirs(base)
23
24
ext = os.path.splitext(path)[1].lower()
25
for places in [
26
os.environ['HOME'],
27
os.path.dirname(os.path.realpath(__file__))
28
]:
29
template = os.path.join(places, 'templates', PLATFORM, 'default' + ext)
30
if os.path.exists(template):
31
shutil.copyfile(template, path)
32
return
33
34
# No template found
35
open(path, 'w').close()
36
37
38
def main():
39
if len(sys.argv) == 1:
40
print("""
41
This script is called like so:
42
43
%s path/to/file.tex another/path/to/a/file.tex ....
44
45
If path/to/file.tex already exists, nothing happens.
46
If path/to/file.tex does not exist, it is created (including the directory that contains it),
47
and if there is a file $HOME/templates/default.tex or /projects/templates/[platform]/default.tex (for tex extension),
48
then that template file is set to the initial contents. """ % sys.argv[0])
49
sys.exit(1)
50
51
for x in sys.argv[1:]:
52
new_file(x)
53
54
55
if __name__ == "__main__":
56
main()
57
58