-
Notifications
You must be signed in to change notification settings - Fork 131
/
project.py
154 lines (125 loc) · 5.04 KB
/
project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: utf-8 -*-
# -- This file is part of the Apio project
# -- (C) 2016-2019 FPGAwars
# -- Author Juan González, Jesús Arroyo
# -- Licence GPLv2
import sys
import json
import click
try:
import ConfigParser
except ImportError:
import configparser as ConfigParser
from os.path import isfile
from apio import util
from apio.resources import Resources
PROJECT_FILENAME = 'apio.ini'
class Project(object):
def __init__(self):
self.board = None
def create_sconstruct(self, project_dir='', sayyes=False):
"""Creates a default SConstruct file"""
project_dir = util.check_dir(project_dir)
sconstruct_name = 'SConstruct'
sconstruct_path = util.safe_join(project_dir, sconstruct_name)
local_sconstruct_path = util.safe_join(
util.get_folder('resources'), sconstruct_name)
if isfile(sconstruct_path):
# -- If sayyes, skip the question
if sayyes:
self._copy_sconstruct_file(sconstruct_name, sconstruct_path,
local_sconstruct_path)
else:
click.secho(
'Warning: {} file already exists'.format(sconstruct_name),
fg='yellow')
if click.confirm('Do you want to replace it?'):
self._copy_sconstruct_file(sconstruct_name,
sconstruct_path,
local_sconstruct_path)
else:
click.secho('Abort!', fg='red')
else:
self._copy_sconstruct_file(sconstruct_name, sconstruct_path,
local_sconstruct_path)
def create_ini(self, board, project_dir='', sayyes=False):
"""Creates a new apio project file"""
project_dir = util.check_dir(project_dir)
ini_path = util.safe_join(project_dir, PROJECT_FILENAME)
# Check board
boards = Resources().boards
if board not in boards.keys():
click.secho(
'Error: no such board \'{}\''.format(board),
fg='red')
sys.exit(1)
if isfile(ini_path):
# -- If sayyes, skip the question
if sayyes:
self._create_ini_file(board, ini_path, PROJECT_FILENAME)
else:
click.secho(
'Warning: {} file already exists'.format(PROJECT_FILENAME),
fg='yellow')
if click.confirm('Do you want to replace it?'):
self._create_ini_file(board, ini_path, PROJECT_FILENAME)
else:
click.secho('Abort!', fg='red')
else:
self._create_ini_file(board, ini_path, PROJECT_FILENAME)
def _create_ini_file(self, board, ini_path, ini_name):
click.secho('Creating {} file ...'.format(ini_name))
with open(ini_path, 'w') as file:
config = ConfigParser.ConfigParser()
config.add_section('env')
config.set('env', 'board', board)
config.write(file)
click.secho(
'File \'{}\' has been successfully created!'.format(
ini_name),
fg='green')
def _copy_sconstruct_file(self, sconstruct_name,
sconstruct_path, local_sconstruct_path):
click.secho('Creating {} file ...'.format(sconstruct_name))
with open(sconstruct_path, 'w') as sconstruct:
with open(local_sconstruct_path, 'r') as local_sconstruct:
sconstruct.write(local_sconstruct.read())
click.secho(
'File \'{}\' has been successfully created!'.format(
sconstruct_name),
fg='green')
def read(self):
"""Read the project config file"""
# -- If no project finel found, just return
if not isfile(PROJECT_FILENAME):
print('Info: No {} file'.format(PROJECT_FILENAME))
return
# -- Read stored board
board = self._read_board()
# -- Update board
self.board = board
if not board:
print('Error: invalid {} project file'.format(
PROJECT_FILENAME))
print('No \'board\' field defined in project file')
sys.exit(1)
def _read_board(self):
board = ''
# -- Read config file: old JSON format
with open(PROJECT_FILENAME, 'r') as f:
try:
data = json.loads(f.read())
board = data.get('board')
except Exception:
pass
# -- Read config file: new CFG format
if board == '':
try:
config = ConfigParser.ConfigParser()
config.read(PROJECT_FILENAME)
board = config.get('env', 'board')
except Exception:
print('Error: invalid {} project file'.format(
PROJECT_FILENAME))
sys.exit(1)
return board